const gulp = require('gulp');
const rename = require('gulp-rename');
const minhtml = require('gulp-htmlmin');
const mincss = require('gulp-clean-css');
const minjs = require('gulp-uglify-es').default;
gulp.task('minhtml', (cd) => {
  gulp.src('./*.html')
    .pipe(minhtml({
      removeComments: true, // 清除HTML注释
      collapseWhitespace: true, // 压缩HTML
      minifyJS: true, // 压缩页面JS
      minifyCSS: true // 压缩页面CSS
    }))
    .pipe(rename({
      suffix: '.min'
    }))
    .pipe(gulp.dest('helper/dist/html'));
  cd();
});

gulp.task('mincss', (cd) => {
  gulp.src('./*.css')
    .pipe(mincss())
    .pipe(rename({
      suffix: '.min'
    }))
    .pipe(gulp.dest('helper/dist/css'));
  cd();
});

gulp.task('minjs', (cd) => {
  gulp.src(['./*.js', '!gulpfile.js'])
    .pipe(minjs())
    .pipe(rename({
      suffix: '.min'
    }))
    .pipe(gulp.dest('helper/dist/js'));
  cd();
});

gulp.task('watchfile', (cd) => {
  gulp.watch('./*.html', gulp.series(['minhtml']));
  gulp.watch('./*.css', gulp.series(['mincss']));
  gulp.watch('./*.js', gulp.series(['minjs']));
  cd();
});
